<?php

/*
Plugin Name: WPS3Slider
Plugin URI: http://www.kevinbradwick.co.uk/2010/06/wps3slider-plugin-for-wordpress/ 
Description: A plugin that integrates the jQuery s3slider plugin
Version: 2.0
Author: Kevin Bradwick
Author URI: http://www.kevinbradwick.co.uk
Licence: GPL2

Copyright 2010 Kevin Bradwick  (email : kbradwick@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as 
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


/**
 * Define some constants
 */
defined('WP_PLUGIN_URL') || define(WP_PLUGIN_URL, WP_CONTENT_URL . '/plugins', true);
define('WP_PLUGIN_BASE_URL', get_bloginfo('url') . '/wp-admin/upload.php?page=wps3slider', true);

$pageGalleries = array();

/**
 * Load the helper file
 */
require_once(dirname(__FILE__).'/wps3helpers.php');

/**
 * Load the admin menus
 */
require_once(dirname(__FILE__).'/wps3admin.php');


/**
 * Register plugin activation
 *
 * Upon registering, the plugin will check for the exsitence of
 * the necessary table(s) and create them if they do not exist
 *
 * @return	null
 */
function wps3_activation()
{
	global $wpdb;
	$schema = str_replace('{PREFIX}', $wpdb->prefix, file_get_contents(dirname(__FILE__).'/assets/schema.sql'));
	$wpdb->query($schema);
}
register_activation_hook(__FILE__, 'wps3_activation');



/**
 * Regiser scripts
 *
 * This will register the javascripts required
 * for the plugin to work. All scripts are placed in
 * the footer for better performance, so make sure
 * the template function wp_footer() is used in your
 * theme files.
 *
 * @return null
 */
function wps3_register_scripts()
{
	wp_register_script('intelislide', WP_PLUGIN_URL . '/wps3slider/assets/intelislide.jquery.min.js', array('jquery'), '1.0',false);
	wp_enqueue_script('intelislide');
}
add_action('init','wps3_register_scripts');


/**
 * Register shortcode
 *
 * This adds the shortcode wps3 to the system so that
 * you can use the wps3 shortcode tag in the theme
 * files.
 *
 * @param	array
 * @return	null
 */
function wps3_shortcode( $params )
{
	if(isset($params['id'])) {
		global $pageGalleries;
		$pageGalleries[] = $params['id'];
		echo wps3_render_slider($params['id']);
	}
}
add_shortcode('wps3','wps3_shortcode');


/**
 * Render galleries
 *
 * This filters a post/page content for the shortcode and
 * renders the appropriate gallery on the page
 *
 * @param	string
 * @return	null
 */
function wps3_filter_content( $content )
{
	global $pageGalleries;
	$pattern = '/\[wps3\sid=\"([0-9]*)\"\]/i';
	$galleries = array();
	
	if(preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
		foreach($matches as $gallery) {
			$slider = wps3_render_slider(trim($gallery[1]));
			if($slider) {
				$content = str_replace('[wps3 id="'.$gallery[1].'"]', $slider, $content);
				$galleries[] = (int)$gallery[1];
			} else {
				$content = preg_replace('/\[wps3\sid=\"'.$gallery[1].'\"\]/', '', $content);
			}
		}
	}
	
	if(count($galleries) > 0)
		$pageGalleries = $galleries;
		
	return $content;
}
add_filter('the_content','wps3_filter_content');


/**
 * Prepeare galleries code into the footer
 */
function wps3_prepare_js( $galleries = array() )
{
	global $pageGalleries, $wpdb;
	
	if(count($pageGalleries) > 0) {
		$galleries = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wps3_galleries WHERE id IN(".implode(',',$pageGalleries).")");
		?>
		<!-- simpleslide -->
		<script type="text/javascript">
		//<![CDATA[
		jQuery(document).ready(function($) {
			<?php foreach($galleries as $gallery) : ?>
			$("#intelislide-<?php echo $gallery->id; ?>").intelislide({
				width:<?php echo $gallery->width; ?>,
				height:<?php echo $gallery->height; ?>,
				tagType:'span',
				transitionSpeed:<?php echo $gallery->transition_speed; ?>,
				timeout:<?php echo $gallery->timeout; ?>
			});
			<?php endforeach; ?>
		});
		//]]>
		</script>
		<?php
	}
}
add_action('wp_footer','wps3_prepare_js');
?>